<script setup>
import { ref } from 'vue'
import { User, Lock, DArrowLeft } from '@element-plus/icons-vue'
import { userLoginService } from '@/api/user.js'
import router from '@/router/index.js'
import { userStore } from '@/stores'
const userstore = userStore()
const modelform = ref({
  username: '',
  password: '',
  repassword: ''
})
//表单校验规则
const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  repassword: [
    {
      required: true,
      // message: '请确认密码',
      trigger: 'change',
      validator: (rule, value, callback) => {
        if (value !== modelform.value.password) {
          callback(new Error('两次密码不匹配'))
        }
        if (value === '') {
          callback(new Error('不能为空'))
        } else {
          callback()
        }
      }
    }
  ]
}
//是否是登陆
const isRegister = ref(true)
const transition = () => {
  isRegister.value = !isRegister.value
  form.value.resetFields()
}
//登陆
const form = ref({})
const Login = async () => {
  form.value.validate()
  const res = await userLoginService(modelform.value)
  if (res.data.code === 1) {
    userstore.setToken(res.data.data.token)
    userstore.setUserInfo(res.data.data)
    ElMessage.success('登陆成功')
    setTimeout(() => {
      router.push('/home')
    }, 500)
  }
}
//注册
const Register = () => {
  // console.log('register')
  form.value.validate()
}
</script>
<template>
  <div class="card" v-if="isRegister">
    <el-row>
      <el-col :span="24">
        <div style="height: 200px"></div>
      </el-col>
    </el-row>
    <el-row :gutter="30">
      <el-col :span="16"> </el-col>
      <el-col :span="8"
        ><el-card class="box-card">
          <template #header>
            <h3>登陆</h3>
          </template>
          <el-form :model="modelform" :rules="rules" ref="form">
            <el-form-item label="" prop="username">
              <el-input
                :prefix-icon="User"
                v-model="modelform.username"
                placeholder="请输入账号"
              ></el-input>
            </el-form-item>
            <el-form-item label="" prop="password">
              <el-input
                :prefix-icon="Lock"
                v-model="modelform.password"
                placeholder="请输入密码"
                type="password"
              ></el-input>
            </el-form-item>
            <el-form-item label="" style="justify-content: space-between">
              <el-space class="ji">
                <el-checkbox label="记住我"></el-checkbox>
                <el-link type="primary" :underline="false">忘记密码？</el-link>
              </el-space>
            </el-form-item>
            <el-form-item label="">
              <el-button type="primary" @click="Login">登陆</el-button>
            </el-form-item>
            <el-form-item label="">
              <el-link :underline="false" type="primary" @click="transition"
                >没有账号？去注册</el-link
              >
            </el-form-item>
          </el-form>
        </el-card></el-col
      >
    </el-row>
  </div>
  <!-- 注册 -->
  <div class="card" v-else>
    <el-row>
      <el-col :span="24">
        <div style="height: 200px"></div>
      </el-col>
    </el-row>
    <el-row :gutter="30">
      <el-col :span="16"> </el-col>
      <el-col :span="8"
        ><el-card class="box-card">
          <template #header>
            <h3>注册</h3>
          </template>
          <el-form :model="modelform" :rules="rules" ref="form">
            <el-form-item label="" prop="username">
              <el-input
                :prefix-icon="User"
                v-model="modelform.username"
                placeholder="请输入账号"
              ></el-input>
            </el-form-item>
            <el-form-item label="" prop="password">
              <el-input
                :prefix-icon="Lock"
                v-model="modelform.password"
                placeholder="请输入密码"
                type="password"
              ></el-input>
            </el-form-item>
            <el-form-item label="" prop="repassword">
              <el-input
                :prefix-icon="Lock"
                v-model="modelform.repassword"
                placeholder="请确认密码"
                type="password"
              ></el-input>
            </el-form-item>
            <el-form-item label="">
              <el-button type="primary" @click="Register">注册</el-button>
            </el-form-item>
            <el-form-item label="">
              <el-link :underline="false" type="primary" @click="transition"
                ><el-icon><DArrowLeft /></el-icon>登陆</el-link
              >
            </el-form-item>
          </el-form>
        </el-card></el-col
      >
    </el-row>
  </div>
</template>

<style lang="css">
#app,
html,
body {
  height: 100%;
}
</style>
<style scoped>
.card {
  width: 100%;
  height: 100%;
  background-image: url(../assets/2.jpg);
  background-size: cover;
  background-color: aqua;
}
.box-card {
  width: 480px;
}
.el-card {
  opacity: 0.9;
}
.el-button {
  width: 100%;
}
.ji {
  width: 100%;
  justify-content: space-between;
}
</style>
